use common_lib::net::http::get;
use serde_json::Value;

fn main() {
    let list = get_crate_version_list("hdrs");
    println!("{:#?}", list);
}

fn get_crate_version_list(crate_name: &str) -> Vec<String> {
    let url = format!("https://crates.io/api/v1/crates/{}", crate_name);
    let res = get(&url).unwrap();
    let json: Value = serde_json::from_str(&res).unwrap();
    let versions = json.get("versions").unwrap();
    if let Value::Array(x) = versions {
        x.iter().fold(Vec::new(), |mut list, value| {
            if let Some(Value::String(v)) = value.get("num") {
                list.push(v.clone());
            }
            list
        })
    } else {
        Vec::new()
    }
}
